package com.rs.qm.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rs.qm.common.annotation.Log;
import com.rs.qm.common.annotation.RedisCache;
import com.rs.qm.common.utils.PageUtils;
import com.rs.qm.common.utils.Query;
import com.rs.qm.common.utils.R;
import com.rs.qm.modules.sys.dao.SysBusinessCategoryDao;
import com.rs.qm.modules.sys.entity.SysBusinessCategoryEntity;
import com.rs.qm.modules.sys.service.SysBusinessCategoryService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;


@Service("sysBusinessCategoryService")
public class SysBusinessCategoryServiceImpl extends ServiceImpl<SysBusinessCategoryDao, SysBusinessCategoryEntity> implements SysBusinessCategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SysBusinessCategoryEntity> page = this.selectPage(
                new Query<SysBusinessCategoryEntity>(params).getPage(),
                new EntityWrapper<SysBusinessCategoryEntity>()
        );

        return new PageUtils(page);
        
    }

    @Override
    @Log
    @RedisCache(cacheName = "SYS-BUSINESS-CATEGORY-ENTITY-ALL")
    public Map<String, Set<String>> queryAllCategory() {
        List<SysBusinessCategoryEntity> categorysListEntity = baseMapper.getAllCategory();
        Map<Long,SysBusinessCategoryEntity> mapIdEntity = new HashMap<>();
        //{"科技类":{"网络科技","生物制药","人工智能"},"服务类":{"餐饮","外卖"}}
        Map<String, Set<String>> categorysMap = new LinkedHashMap<>();
        categorysListEntity.stream().forEach(sysBusinessCategoryEntity -> {
            mapIdEntity.put(sysBusinessCategoryEntity.getCategoryId(),sysBusinessCategoryEntity);
            if("0".equals(sysBusinessCategoryEntity.getParentId())){
                categorysMap.put(sysBusinessCategoryEntity.getName(),new HashSet<String>());
            }
        });
        for (Long key : mapIdEntity.keySet()) {
            SysBusinessCategoryEntity entity = mapIdEntity.get(key);
            if(!"0".equals(entity.getParentId())){
                String parentNameKey = mapIdEntity.get(Long.valueOf(entity.getParentId())).getName();
                categorysMap.get(parentNameKey).add(entity.getName());
            }
        }
        return categorysMap;
    }

}